package edu.unindra.company.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;

import com.toedter.calendar.JDateChooser;

import edu.unindra.company.dao.entity.Empl;
import edu.unindra.company.dao.impl.EmployeeImpl;

import java.awt.Font;
import java.awt.Color;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class MainEmployeeAdd extends JDialog {

	private static final long serialVersionUID = 1L;

	private FormMainEmployee parent;

	private JPanel jContentPane = null;
	private JButton btnTambah = null;
	private JLabel lblNim;

	private JTextField txtNim;
	private JButton btnReset;
	private JButton btnBatal;
	private JLabel lblNama;
	private JLabel lblKontak;
	private JLabel lblAlamat;
	private JTextArea textArea;
	private JLabel lblMasaKerja;
	private JLabel lblTempatLahir;
	private JLabel lblTglLahir;
	private JLabel lblJenisKelamin;
	private JLabel lblJabatan;
	private JLabel lblPhoto;
	private JButton btnBrowser;
	private JTextField txtNama;
	private JTextField txtTmptLahir;
	@SuppressWarnings("rawtypes")
	private JComboBox comboBox;
	private JTextField txtJabatan;
	private JDateChooser dateChooser;
	private JTextField txtMasaKerja;
	private JTextField txtKontak;
	private JTextField txtPhoto;

	@SuppressWarnings("unused")
	private String invalidMessage = "";

	private JFileChooser choser;
	private DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");

	private Empl em = null;

	private EmployeeImpl emplIm = null;

	public MainEmployeeAdd(FormMainEmployee parent) {
		this.parent = parent;

		emplIm = new EmployeeImpl();
		choser = new JFileChooser();

		setModal(true);
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(2);
		setTitle("Tambah Data Employee");
		setContentPane(getJContentPane());

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 560) / 2, (screenSize.height - 418) / 2,
				560, 402);
	}

	private JPanel getJContentPane() {
		if (this.jContentPane == null) {
			GridBagConstraints gbc_lblNim = new GridBagConstraints();
			gbc_lblNim.gridx = 1;
			gbc_lblNim.anchor = GridBagConstraints.WEST;
			gbc_lblNim.insets = new Insets(2, 2, 5, 5);
			gbc_lblNim.weightx = 0.0D;
			gbc_lblNim.gridy = 0;
			this.lblNim = new JLabel();
			lblNim.setHorizontalAlignment(SwingConstants.LEFT);
			this.lblNim.setText("Nim");
			this.jContentPane = new JPanel();
			GridBagLayout gbl_jContentPane = new GridBagLayout();
			gbl_jContentPane.rowHeights = new int[] { 0, 0, 0, 29, 0, 0, 0, 0,
					0, 0, 0 };
			gbl_jContentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
					0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 };
			gbl_jContentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
					0.0, 0.0 };
			this.jContentPane.setLayout(gbl_jContentPane);
			GridBagConstraints gbc_txtNim = new GridBagConstraints();
			gbc_txtNim.gridwidth = 3;
			gbc_txtNim.insets = new Insets(0, 0, 5, 5);
			gbc_txtNim.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtNim.gridx = 3;
			gbc_txtNim.gridy = 0;
			jContentPane.add(getTxtCardNumber(), gbc_txtNim);
			this.jContentPane.add(this.lblNim, gbc_lblNim);
			GridBagConstraints gbc_lblNama = new GridBagConstraints();
			gbc_lblNama.anchor = GridBagConstraints.WEST;
			gbc_lblNama.insets = new Insets(0, 0, 5, 5);
			gbc_lblNama.gridx = 1;
			gbc_lblNama.gridy = 1;
			jContentPane.add(getLblNama(), gbc_lblNama);
			GridBagConstraints gbc_txtNama = new GridBagConstraints();
			gbc_txtNama.gridwidth = 3;
			gbc_txtNama.insets = new Insets(0, 0, 5, 5);
			gbc_txtNama.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtNama.gridx = 3;
			gbc_txtNama.gridy = 1;
			jContentPane.add(getTxtNama(), gbc_txtNama);
			GridBagConstraints gbc_lblTempatLahir = new GridBagConstraints();
			gbc_lblTempatLahir.anchor = GridBagConstraints.WEST;
			gbc_lblTempatLahir.insets = new Insets(0, 0, 5, 5);
			gbc_lblTempatLahir.gridx = 1;
			gbc_lblTempatLahir.gridy = 2;
			jContentPane.add(getLblTempatLahir(), gbc_lblTempatLahir);
			GridBagConstraints gbc_txtTmptLahir = new GridBagConstraints();
			gbc_txtTmptLahir.gridwidth = 3;
			gbc_txtTmptLahir.insets = new Insets(0, 0, 5, 5);
			gbc_txtTmptLahir.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtTmptLahir.gridx = 3;
			gbc_txtTmptLahir.gridy = 2;
			jContentPane.add(getTxtTmptLahir(), gbc_txtTmptLahir);
			GridBagConstraints gbc_lblTglLahir = new GridBagConstraints();
			gbc_lblTglLahir.anchor = GridBagConstraints.WEST;
			gbc_lblTglLahir.insets = new Insets(0, 0, 5, 5);
			gbc_lblTglLahir.gridx = 1;
			gbc_lblTglLahir.gridy = 3;
			jContentPane.add(getLblTglLahir(), gbc_lblTglLahir);
			GridBagConstraints gbc_dateChooser = new GridBagConstraints();
			gbc_dateChooser.gridwidth = 3;
			gbc_dateChooser.insets = new Insets(0, 0, 5, 5);
			gbc_dateChooser.fill = GridBagConstraints.HORIZONTAL;
			gbc_dateChooser.gridx = 3;
			gbc_dateChooser.gridy = 3;
			jContentPane.add(getDateChooser(), gbc_dateChooser);
			GridBagConstraints gbc_lblJenisKelamin = new GridBagConstraints();
			gbc_lblJenisKelamin.anchor = GridBagConstraints.WEST;
			gbc_lblJenisKelamin.insets = new Insets(0, 0, 5, 5);
			gbc_lblJenisKelamin.gridx = 1;
			gbc_lblJenisKelamin.gridy = 4;
			jContentPane.add(getLblJenisKelamin(), gbc_lblJenisKelamin);
			GridBagConstraints gbc_comboBox = new GridBagConstraints();
			gbc_comboBox.gridwidth = 3;
			gbc_comboBox.insets = new Insets(0, 0, 5, 5);
			gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
			gbc_comboBox.gridx = 3;
			gbc_comboBox.gridy = 4;
			jContentPane.add(getComboBox(), gbc_comboBox);
			GridBagConstraints gbc_lblJabatan = new GridBagConstraints();
			gbc_lblJabatan.anchor = GridBagConstraints.WEST;
			gbc_lblJabatan.insets = new Insets(0, 0, 5, 5);
			gbc_lblJabatan.gridx = 1;
			gbc_lblJabatan.gridy = 5;
			jContentPane.add(getLblJabatan(), gbc_lblJabatan);
			GridBagConstraints gbc_txtJabatan = new GridBagConstraints();
			gbc_txtJabatan.gridwidth = 3;
			gbc_txtJabatan.insets = new Insets(0, 0, 5, 5);
			gbc_txtJabatan.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtJabatan.gridx = 3;
			gbc_txtJabatan.gridy = 5;
			jContentPane.add(getTxtJabatan(), gbc_txtJabatan);
			GridBagConstraints gbc_lblMasaKerja = new GridBagConstraints();
			gbc_lblMasaKerja.anchor = GridBagConstraints.WEST;
			gbc_lblMasaKerja.insets = new Insets(0, 0, 5, 5);
			gbc_lblMasaKerja.gridx = 1;
			gbc_lblMasaKerja.gridy = 6;
			jContentPane.add(getLblMasaKerja(), gbc_lblMasaKerja);
			GridBagConstraints gbc_txtMasaKerja = new GridBagConstraints();
			gbc_txtMasaKerja.gridwidth = 3;
			gbc_txtMasaKerja.insets = new Insets(0, 0, 5, 5);
			gbc_txtMasaKerja.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtMasaKerja.gridx = 3;
			gbc_txtMasaKerja.gridy = 6;
			jContentPane.add(getTextField_3_1(), gbc_txtMasaKerja);
			GridBagConstraints gbc_lblKontak = new GridBagConstraints();
			gbc_lblKontak.anchor = GridBagConstraints.WEST;
			gbc_lblKontak.insets = new Insets(0, 0, 5, 5);
			gbc_lblKontak.gridx = 1;
			gbc_lblKontak.gridy = 7;
			jContentPane.add(getLblKontak(), gbc_lblKontak);
			GridBagConstraints gbc_txtKontak = new GridBagConstraints();
			gbc_txtKontak.gridwidth = 3;
			gbc_txtKontak.insets = new Insets(0, 0, 5, 5);
			gbc_txtKontak.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtKontak.gridx = 3;
			gbc_txtKontak.gridy = 7;
			jContentPane.add(getTxtKontak(), gbc_txtKontak);
			GridBagConstraints gbc_lblPhoto = new GridBagConstraints();
			gbc_lblPhoto.anchor = GridBagConstraints.WEST;
			gbc_lblPhoto.insets = new Insets(0, 0, 5, 5);
			gbc_lblPhoto.gridx = 1;
			gbc_lblPhoto.gridy = 8;
			jContentPane.add(getLblPhoto(), gbc_lblPhoto);
			GridBagConstraints gbc_txtPhoto = new GridBagConstraints();
			gbc_txtPhoto.gridwidth = 3;
			gbc_txtPhoto.insets = new Insets(0, 0, 5, 5);
			gbc_txtPhoto.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtPhoto.gridx = 3;
			gbc_txtPhoto.gridy = 8;
			jContentPane.add(getTxtPhoto(), gbc_txtPhoto);
			GridBagConstraints gbc_btnBrowser = new GridBagConstraints();
			gbc_btnBrowser.insets = new Insets(0, 0, 5, 0);
			gbc_btnBrowser.gridx = 6;
			gbc_btnBrowser.gridy = 8;
			jContentPane.add(getBtnBrowser(), gbc_btnBrowser);
			GridBagConstraints gbc_lblAlamat = new GridBagConstraints();
			gbc_lblAlamat.anchor = GridBagConstraints.NORTHWEST;
			gbc_lblAlamat.insets = new Insets(0, 0, 5, 5);
			gbc_lblAlamat.gridx = 1;
			gbc_lblAlamat.gridy = 9;
			jContentPane.add(getLblAlamat(), gbc_lblAlamat);
			GridBagConstraints gbc_textArea = new GridBagConstraints();
			gbc_textArea.gridwidth = 3;
			gbc_textArea.insets = new Insets(0, 0, 5, 5);
			gbc_textArea.fill = GridBagConstraints.BOTH;
			gbc_textArea.gridx = 3;
			gbc_textArea.gridy = 9;
			jContentPane.add(getTextArea(), gbc_textArea);
			GridBagConstraints gbc_btnReset = new GridBagConstraints();
			gbc_btnReset.insets = new Insets(0, 0, 0, 5);
			gbc_btnReset.gridx = 4;
			gbc_btnReset.gridy = 10;
			jContentPane.add(getBtnReset(), gbc_btnReset);
			GridBagConstraints gbc_btnBatal = new GridBagConstraints();
			gbc_btnBatal.insets = new Insets(0, 0, 0, 5);
			gbc_btnBatal.gridx = 5;
			gbc_btnBatal.gridy = 10;
			jContentPane.add(getBtnBatal(), gbc_btnBatal);
			GridBagConstraints gbc_btnTambah = new GridBagConstraints();
			gbc_btnTambah.gridx = 6;
			gbc_btnTambah.insets = new Insets(2, 2, 2, 0);
			gbc_btnTambah.anchor = 13;
			gbc_btnTambah.gridy = 10;
			this.jContentPane.add(getBtnTambah(), gbc_btnTambah);
		}
		return this.jContentPane;
	}

	private JButton getBtnTambah() {
		if (this.btnTambah == null) {
			this.btnTambah = new JButton();
			this.btnTambah.setText("Tambah");
			this.btnTambah.setToolTipText("Insert");
			this.btnTambah.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (isDataEmployeeValid()) {
						int retVal = 0;
						em = new Empl();

						em.setNim(Integer.valueOf(txtNim.getText()));
						em.setNama(txtNama.getText());
						em.setTeTgl(txtTmptLahir.getText());
						em.setTaTgl(dateFormat.format(dateChooser.getDate()));
						em.setjKelamin(comboBox.getSelectedItem().toString());
						em.setJabatan(txtJabatan.getText());
						em.setmKerja(Integer.valueOf(txtMasaKerja.getText()));
						em.setKontak(Integer.valueOf(txtKontak.getText()));
						em.setPhoto(txtPhoto.getText());
						em.setAlamat(textArea.getText());

						try {
							retVal = emplIm.insert(em);
						} catch (SQLException e1) {
							e1.printStackTrace();
						}

						if (retVal > -1) {
							JOptionPane
									.showMessageDialog(MainEmployeeAdd.this,
											"Proses penambahan data karyawan baru berhasil");
							parent.getModelTable().addEmployee(em);
							MainEmployeeAdd.this.setVisible(false);

						} else {
							JOptionPane.showMessageDialog(MainEmployeeAdd.this,
									"Proses input data karyawan baru problem");
						}
					} else {
						JOptionPane.showMessageDialog(MainEmployeeAdd.this,
								"Proses penambahan data karyawan baru problem");

					}

				}
			});
		}
		return this.btnTambah;
	}

	JTextField getTxtCardNumber() {
		if (txtNim == null) {
			txtNim = new JTextField();
			txtNim.setColumns(10);
		}
		return txtNim;
	}

	private JButton getBtnReset() {
		if (btnReset == null) {
			btnReset = new JButton();
			btnReset.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					buttonResetActionPerformed(e);
				}
			});
			btnReset.setToolTipText("Insert");
			btnReset.setText("Reset");
		}
		return btnReset;
	}

	private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {
		reset();
	}

	private void reset() {
		txtNim.setText("");
		txtNama.setText("");
		txtTmptLahir.setText("");
		txtJabatan.setText("");
		txtKontak.setText("");
		txtMasaKerja.setText("");
		txtPhoto.setText("");
		textArea.setText("");

		comboBox.setSelectedIndex(0);
		setFocusable(true);
	}

	private JButton getBtnBatal() {
		if (btnBatal == null) {
			btnBatal = new JButton();
			btnBatal.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					buttonBatalActionPerformed(e);
				}
			});
			btnBatal.setToolTipText("Insert");
			btnBatal.setText("Batal");
		}
		return btnBatal;
	}

	private void buttonBatalActionPerformed(java.awt.event.ActionEvent evt) {
		MainEmployeeAdd.this.setVisible(false);
	}

	private JLabel getLblNama() {
		if (lblNama == null) {
			lblNama = new JLabel();
			lblNama.setText("Nama");
		}
		return lblNama;
	}

	private JLabel getLblKontak() {
		if (lblKontak == null) {
			lblKontak = new JLabel();
			lblKontak.setText("Kontak");
		}
		return lblKontak;
	}

	private JLabel getLblAlamat() {
		if (lblAlamat == null) {
			lblAlamat = new JLabel();
			lblAlamat.setVerticalAlignment(SwingConstants.TOP);
			lblAlamat.setText("Alamat");
		}
		return lblAlamat;
	}

	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}

	private JLabel getLblMasaKerja() {
		if (lblMasaKerja == null) {
			lblMasaKerja = new JLabel();
			lblMasaKerja.setText("Masa Kerja");
		}
		return lblMasaKerja;
	}

	private JLabel getLblTempatLahir() {
		if (lblTempatLahir == null) {
			lblTempatLahir = new JLabel();
			lblTempatLahir.setText("Tempat Lahir");
		}
		return lblTempatLahir;
	}

	private JLabel getLblTglLahir() {
		if (lblTglLahir == null) {
			lblTglLahir = new JLabel();
			lblTglLahir.setText("Tgl lahir");
		}
		return lblTglLahir;
	}

	private JLabel getLblJenisKelamin() {
		if (lblJenisKelamin == null) {
			lblJenisKelamin = new JLabel();
			lblJenisKelamin.setText("Jenis Kelamin");
		}
		return lblJenisKelamin;
	}

	private JLabel getLblJabatan() {
		if (lblJabatan == null) {
			lblJabatan = new JLabel();
			lblJabatan.setText("Jabatan");
		}
		return lblJabatan;
	}

	private JLabel getLblPhoto() {
		if (lblPhoto == null) {
			lblPhoto = new JLabel();
			lblPhoto.setText("Photo");
		}
		return lblPhoto;
	}

	private JButton getBtnBrowser() {
		if (btnBrowser == null) {
			btnBrowser = new JButton();
			btnBrowser.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					buttonBrowserActionPerformed(e);
				}
			});
			btnBrowser.setToolTipText("Insert");
			btnBrowser.setText("Browser");
		}
		return btnBrowser;
	}

	protected void buttonBrowserActionPerformed(ActionEvent e) {
		int ren = choser.showOpenDialog(this);
		if (ren == JFileChooser.APPROVE_OPTION) {
			txtPhoto.setText(choser.getSelectedFile().getAbsolutePath());
		}
	}

	private JTextField getTxtNama() {
		if (txtNama == null) {
			txtNama = new JTextField();
			txtNama.setColumns(10);
		}
		return txtNama;
	}

	private JTextField getTxtTmptLahir() {
		if (txtTmptLahir == null) {
			txtTmptLahir = new JTextField();
			txtTmptLahir.setColumns(10);
		}
		return txtTmptLahir;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();
			comboBox.setModel(new DefaultComboBoxModel(new String[] {
					"laki - laki", "perempuan" }));
		}
		return comboBox;
	}

	private JTextField getTxtJabatan() {
		if (txtJabatan == null) {
			txtJabatan = new JTextField();
			txtJabatan.setColumns(10);
		}
		return txtJabatan;
	}

	private JDateChooser getDateChooser() {
		if (dateChooser == null) {
			dateChooser = new JDateChooser();
			dateChooser.getSpinner().setForeground(Color.BLACK);
			dateChooser.setFont(new Font("Tahoma", Font.BOLD, 11));
		}
		return dateChooser;
	}

	private JTextField getTextField_3_1() {
		if (txtMasaKerja == null) {
			txtMasaKerja = new JTextField();
			txtMasaKerja.setColumns(10);
		}
		return txtMasaKerja;
	}

	private JTextField getTxtKontak() {
		if (txtKontak == null) {
			txtKontak = new JTextField();
			txtKontak.setColumns(10);
		}
		return txtKontak;
	}

	private JTextField getTxtPhoto() {
		if (txtPhoto == null) {
			txtPhoto = new JTextField();
			txtPhoto.setColumns(10);
		}
		return txtPhoto;
	}

	private boolean isDataEmployeeValid() {
		if (txtNim.getText().trim().length() == 0) {
			invalidMessage = "Nim Karyawan masih kosong";
			return false;
		}

		if (txtNama.getText().trim().length() == 0) {
			invalidMessage = "Nama Karyawan masih kosong";
			return false;
		}

		if (txtTmptLahir.getText().trim().length() == 0) {
			invalidMessage = "Tempat Lahir masih kosong";
			return false;
		}

		if (txtJabatan.getText().trim().length() == 0) {
			invalidMessage = "Jabatan Karyawan masih kosong";
			return false;
		}

		if (txtMasaKerja.getText().trim().length() == 0) {
			invalidMessage = "Masa Kerja Karyawan masih kosong";
			return false;
		}

		if (textArea.getText().trim().length() == 0) {
			invalidMessage = "Nim Karyawan masih kosong";
			return false;
		}

		return true;
	}

	public Empl getEm() {
		return em;
	}

	public void setEm(Empl em) {
		this.em = em;
	}
}